<template>
	<view style="min-height: 100vh;">
		<u-navbar title="材料报价" :placeholder="true" :autoBack="true"></u-navbar>
		<view style="padding-bottom: 200rpx;font-size: 26rpx;">
			<view v-for="(v,i) in RBLIST" :key="i"
				style="padding:20rpx 0;text-align:center;border-bottom:1rpx solid #eee;display:flex;align-items: center;justify-content: space-between;">
				<text style="width: 50%;">{{v.firstMaterialName}}</text>
				<text style="width: 20%;">{{v.firstMaterialPrice}}</text>
				<view style="width: 30%;">
					<uni-number-box v-model="v.count"></uni-number-box>
				</view>
			</view>
		</view>
		<view style="width: 500rpx;position: fixed;bottom:100rpx;left:125rpx;z-index: 100;">
			 <u-button shape='circle'  @click='createOrder' :color="color">创建订单</u-button>
		</view>
	</view>
</template>

<script>
	let app = getApp();
	import Decimal  from 'decimal.js';
	export default{
		onLoad(option){
			this.id  = option.id;
			this.floorTileNum = option.floorTileNum;
			this.wallboardNum = option.wallboardNum;
			this.brandPartyId = option.brandPartyId;
			this.areaData = option.areaData;
			if(this.brandPartyId  ==1){
				this.getSaveMaterial();
			}else{
				this.getList();
			}
			
		},
		data(){
			return{
				color:app.globalData.defaultColor,
				brandPartyId: null,
				isFirst: null,
				id:'',
				saveMaterial:[],
				RBLIST: [],
				wallboardNum:0,
				floorTileNum:0,
				areaData:0,
				BUSYLIST:[]
				
			}
		},
		methods:{
			createOrder(){
			if (this.brandPartyId == 2 || this.brandPartyId == 1) {
				let arr = [];
				let totalPrice = 0;
				this.RBLIST.map(el => {
					if (el.count > 0) {
						totalPrice += Decimal(el.count).mul(Decimal(el.firstMaterialPrice)).toNumber();
						arr.push({
							'buyCount': el.count,
							'name': el.classificationName,
							'classificationId': el.classificationId,
							'salePrice': el.firstMaterialPrice
						})
					}
				})
				if(totalPrice == 0){
					uni.showToast({
						title:'请选择材料后再下单!',
						icon:'error'
					})
					return;
				}
				let data = {
					assignTaskId: this.id,
					list: arr,
					totalPrice: totalPrice,
					state: 1,
				}
				uni.$u.http.post('/materials/addWechatOrderInfo', data).then(res => {
					if (res) {
						uni.$u.toast('下单成功!');
						setTimeout(()=>{
							uni.redirectTo({
								url:'/pages/mine/index'
							})
						},1000);
					}else{
						uni.$u.toast('操作失败,请联系管理员!');
					}
				})
			} 
			},
			getSaveMaterial(){
				uni.$u.http.get('/materials/getScaleMaterialsRecordsList', {
					params: {
						assignTaskId: this.id,
					 
					}
				}).then(async res => {
					if (res) {
				    this.saveMaterial = res;
				    await this.getList();
					}
				})
			},
			getList(){
				uni.$u.http.get('/materials/getMaterialsByDxf', {
					params: {
						assignTaskId: this.id,
						pageSize: 100000,
						pageNumber: 1
					}
				}).then(res => {
					if (res) {
						if (this.brandPartyId == 2) {
							res.data.map(el => {
								if (el.classificationId == 360) {
									el.count = this.wallboardNum;
								} else if (el.classificationId == 361) {
									el.count = this.wallboardNum;
								} else if (el.classificationId == 369) {
									el.count = this.floorTileNum
								} else {
									el.count = 0;
								}
								this.RBLIST.push(el)
							})
						} else if (this.brandPartyId == 1) {
							let data = res.data;
							let saveMaterial = this.saveMaterial;
							for(let i = 0;i<saveMaterial.length ;i++){
								for(let j =0;j<data.length ;j++){
									if(saveMaterial[i].classificationId == data[j].classificationId){
										data[j].count = saveMaterial[i].buyCount;
										console.log(data[j]);
									}else{
										data[i].count = 0;
									}
								}
								
							}
							 
						 
				            this.RBLIST  = res.data;
						 
				  
						}
				
						 
					}
				})
			}
		}
	}
</script>

<style>
</style>